package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         189,
		Discription: "旋转数组",
		Level:       2,
		Labels: map[string]int{
			"数学": 1,
		},
	})
}

func RotateArray(nums []int, k int) {
	//<=1不用旋转
	if len(nums) <= 1 {
		return
	}

	reverse := func(start int, end int) {
		mid := (start+end)/2 - start
		for i := 0; i <= mid; i++ {
			nums[start+i], nums[end-i] = nums[end-i], nums[start+i]
		}
	}

	k = k % len(nums)
	//如果等效于不用旋转得直接return，要不然后面会越界
	if k == 0 {
		return
	}
	reverse(0, len(nums)-1)
	reverse(0, k-1)
	reverse(k, len(nums)-1)
}
